﻿using Bot.Domain;
using System;
using System.Collections.Generic;
using System.Text;
using AutoMapper;
using Bot.Application.Dto;
using Bot.Domain.Entities;
using Bot.Domain.Enum;
using Newtonsoft.Json;
using Bot.Domain.Bo;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Linq;
using Bot.Application.Dto.AgentBot;

namespace Bot.Application
{
    public class DtoProfile : Profile,IProfile
    {
        public DtoProfile()
        {
            //CreateMap<ChatbotCreateDto,Chatbot>()
            //    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));

            CreateMap<Image, ImageCreateDto>().ReverseMap();          
       
            CreateMap<GreetingMessageInChannel, GreetingMessageInChannelCreateDto>().ReverseMap();
            CreateMap<NoAnswerMessageInChannel, NoAnswerMessageInChannelCreateDto>().ReverseMap();
            CreateMap<MessageAfterSeveralConsecutivePossibleAnswersInChannel, MessageAfterSeveralConsecutivePossibleAnswersInChannelCreateDto>().ReverseMap();
            CreateMap<Webhook, WebhookCreateDto>()
                .ForMember(dest => dest.Headers, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<Dictionary<string, string>>(src.Header)));
            CreateMap<WebhookCreateDto, Webhook>()
                .ForMember(dest => dest.Header, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Headers)));
            CreateMap<ChatbotCreateDto, Chatbot>();
            CreateMap<Chatbot, ChatbotDto>();          
            CreateMap<Image, ImageDto>();
            CreateMap<GreetingMessageInChannel, GreetingMessageInChannelDto>();
            CreateMap<NoAnswerMessageInChannel, Dto.NoAnswerMessageInChannelDto>();
            CreateMap<MessageAfterSeveralConsecutivePossibleAnswersInChannel, MessageAfterSeveralConsecutivePossibleAnswersInChannelDto>();

            CreateMap<ChatbotUpdateDto, ChatbotUpdateBo>();
            CreateMap<ImageUpdateDto, ImageUpdateBo>();
            CreateMap<GreetingMessageInChannelUpdateDto, GreetingMessageInChannelUpdateBo>();
            CreateMap<NoAnswerMessageInChannelUpdateDto, NoAnswerMessageInChannelUpdateBo>();
            CreateMap<MessageAfterSeveralConsecutivePossibleAnswersInChannelUpdateDto, MessageAfterSeveralConsecutivePossibleAnswersInChannelUpdateBo>();
            CreateMap<WebhookUpdateDto, WebhookUpdateBo>();



            CreateMap<ChatbotUpdateBo, Chatbot>().ReverseMap();
            CreateMap<ImageUpdateBo, Image>().ReverseMap();
            CreateMap<GreetingMessageInChannelUpdateBo, GreetingMessageInChannel>().ReverseMap();
            CreateMap<NoAnswerMessageInChannelUpdateBo, NoAnswerMessageInChannel>().ReverseMap();
            CreateMap<MessageAfterSeveralConsecutivePossibleAnswersInChannelUpdateBo, MessageAfterSeveralConsecutivePossibleAnswersInChannel>().ReverseMap();
            CreateMap<WebhookUpdateBo, Webhook>()
                .ForMember(dest => dest.Header, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Headers)));
            CreateMap<Webhook, WebhookUpdateBo > ()
                .ForMember(dest => dest.Headers, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Header)));
            CreateMap<ResponseUpdateBo, Response>()
                .ForMember(dest => dest.TextVariants, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.TextVariants)))
                .ForMember(dest => dest.HtmlTextVariants, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.HtmlTextVariants)));
            CreateMap<Response, ResponseUpdateBo>()
                .ForMember(dest => dest.TextVariants, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.TextVariants)))
                .ForMember(dest => dest.HtmlTextVariants, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.HtmlTextVariants)));
            CreateMap<ResponseButtonUpdateBo, ResponseButton>().ReverseMap();

            CreateMap<PrebuiltEntity, Entity>()
                 .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                 .ForMember(dest => dest.Type, opt => opt.MapFrom(src => EnumEntityType.systemEntity));

            CreateMap<EntityCreateDto, Entity>().ReverseMap();
            CreateMap<EntityKeywordCreateDto, EntityKeyword>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Synonyms)));
            CreateMap<EntityKeyword, EntityKeywordCreateDto > ()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Synonyms)));

            CreateMap<Entity, Dto.EntityDto>();
            CreateMap<EntityKeyword, EntityKeywordDto>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Synonyms)));

            CreateMap<EntityUpdateDto, EntityUpdateBo>().ReverseMap();
            CreateMap<EntityKeywordUpdateDto, EntityKeywordUpdateBo>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Synonyms)));
            CreateMap<EntityKeywordUpdateBo, EntityKeywordUpdateDto>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Synonyms)));
            CreateMap<EntityUpdateBo, Entity>().ReverseMap();
            CreateMap<EntityKeywordUpdateBo, EntityKeyword>().ReverseMap();

            CreateMap<Chatbot, Domain.Bot.BotDto>()
                .ForMember(dest => dest.SiteId, opt => opt.MapFrom(src => src.SiteId))
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.Engine, opt => opt.MapFrom(src => src.EngineType.ToString()))
                .ForMember(dest => dest.BotEngineAppId, opt => opt.MapFrom(src => src.DialogFlowProjectId));

            //engine map
            CreateMap<Entity, Domain.EntityDto>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.BotId, opt => opt.MapFrom(src => src.BotId))
                .ForMember(dest => dest.EntityType, opt => opt.MapFrom(src => src.Type))
                .ForMember(dest => dest.EntityName, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.DisplayName, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.MappingToBotEngine, opt => opt.MapFrom(src => src.MappingToDialogflowId))
                .ForMember(dest => dest.ListEntityItems, opt => opt.MapFrom(src => src.Keywords));

            CreateMap<EntityKeyword, Domain.ListEntityItemDto>()
                .ForMember(dest => dest.EntityId, opt => opt.MapFrom(src => src.EntityId))
                .ForMember(dest => dest.KeyWord, opt => opt.MapFrom(src => src.Content))
                .ForMember(dest => dest.SynonymsOfKeyWord, opt => opt.MapFrom(src => src.Synonyms));

            CreateMap<Intent, Domain.IntentDto>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.CategoryId, opt => opt.MapFrom(src => src.CategoryId))
                .ForMember(dest => dest.MappingToBotEngine, opt => opt.MapFrom(src => src.MappingToDialogflowId))
                .ForMember(dest => dest.Questions, opt => opt.MapFrom(src => FormatQuestions(src.Questions)))
                .ForMember(dest => dest.Answer, opt => opt.MapFrom(src => FormatAnswerInChannels(src.AnswerInChannels)));


            CreateMap<CategoryCreateDto, Category>().ReverseMap();
            CreateMap<CategoryUpdateDto, CategoryUpdateBo>().ReverseMap();
            CreateMap<CategoryUpdateBo, Category>().ReverseMap();
            CreateMap<Category, Dto.CategoryDto>();

            CreateMap<IntentCreateDto, Intent>().ReverseMap();
            CreateMap<IntentQuestionCreateDto, IntentQuestion>().ReverseMap();
            CreateMap<IntentQuestionSelectedKeywordCreateDto, IntentQuestionSelectedKeyword>().ReverseMap();
            CreateMap<IntentAnswerInChannelCreateDto, IntentAnswerInChannel>().ReverseMap();            
            CreateMap<AnswerInChannelAuthenticationRequestCreateDto, AnswerInChannelAuthenticationRequest>().ReverseMap();
            CreateMap<AnswerInChannelLocationRequestCreateDto, AnswerInChannelLocationRequest>().ReverseMap();
            CreateMap<AnswerInChannelFormCreateDto, AnswerInChannelForm>().ReverseMap();
            CreateMap<AnswerInChannelFormFieldCreateDto, AnswerInChannelFormField>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Options)));
            CreateMap<AnswerInChannelFormField, AnswerInChannelFormFieldCreateDto>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<AnswerInChannelPromptCreateDto, AnswerInChannelPrompt>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Options)));
            CreateMap<AnswerInChannelPrompt, AnswerInChannelPromptCreateDto>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<QuickReplyCreateDto, QuickReply>().ReverseMap();
            CreateMap<QuickReplyItemCreateDto, QuickReplyItem>().ReverseMap();
            CreateMap<ResponseCreateDto, Response>()
                .ForMember(dest => dest.TextVariants, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.TextVariants)))
                .ForMember(dest => dest.HtmlTextVariants, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.HtmlTextVariants)));
            CreateMap<Response, ResponseCreateDto>()
              .ForMember(dest => dest.TextVariants, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.TextVariants)))
              .ForMember(dest => dest.HtmlTextVariants, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.HtmlTextVariants)));
            CreateMap<ResponseButtonCreateDto, ResponseButton>().ReverseMap();

            CreateMap<IntentUpdateDto, IntentUpdateBo>().ReverseMap();
            CreateMap<IntentQuestionUpdateDto, IntentQuestionUpdateBo>().ReverseMap();
            CreateMap<IntentQuestionSelectedKeywordUpdateDto, IntentQuestionSelectedKeywordUpdateBo>().ReverseMap();
            CreateMap<IntentAnswerInChannelUpdateDto, IntentAnswerInChannelUpdateBo>().ReverseMap();
            CreateMap<AnswerInChannelAuthenticationRequestUpdateDto, AnswerInChannelAuthenticationRequestUpdateBo>().ReverseMap();
            CreateMap<AnswerInChannelLocationRequestUpdateDto, AnswerInChannelLocationRequestUpdateBo>().ReverseMap();
            CreateMap<AnswerInChannelFormUpdateDto, AnswerInChannelFormUpdateBo>().ReverseMap();
            CreateMap<AnswerInChannelFormFieldUpdateDto, AnswerInChannelFormFieldUpdateBo>(); 
            CreateMap<AnswerInChannelFormFieldUpdateBo, AnswerInChannelFormFieldUpdateDto>();
            CreateMap<AnswerInChannelPromptUpdateDto, AnswerInChannelPromptUpdateBo>();
            CreateMap<AnswerInChannelPromptUpdateBo, AnswerInChannelPromptUpdateDto>();
            CreateMap<QuickReplyUpdateDto, QuickReplyUpdateBo>().ReverseMap();
            CreateMap<QuickReplyItemUpdateDto, QuickReplyItemUpdateBo>().ReverseMap();
            CreateMap<ResponseUpdateDto, ResponseUpdateBo>();
            CreateMap<ResponseButtonUpdateDto, ResponseButtonUpdateBo>().ReverseMap();

            CreateMap<Intent, Dto.IntentDto>();
            CreateMap<IntentQuestion, IntentQuestionDto>();
            CreateMap<IntentQuestionSelectedKeyword, IntentQuestionSelectedKeywordDto>();
            CreateMap<IntentAnswerInChannel, IntentAnswerInChannelDto>();
            CreateMap<AnswerInChannelAuthenticationRequest, AnswerInChannelAuthenticationRequestDto>();
            CreateMap<AnswerInChannelLocationRequest, AnswerInChannelLocationRequestDto>();
            CreateMap<AnswerInChannelForm, AnswerInChannelFormDto>();
            CreateMap<AnswerInChannelFormField, AnswerInChannelFormFieldDto>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<AnswerInChannelPrompt, AnswerInChannelPromptDto>()
               .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<QuickReply, Dto.QuickReplyDto>();
            CreateMap<QuickReplyItem, QuickReplyItemDto>();
            CreateMap<Response, ResponseDto>()
                .ForMember(dest => dest.TextVariants, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.TextVariants)))
                .ForMember(dest => dest.HtmlTextVariants, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.HtmlTextVariants)));
            CreateMap<ResponseButton, ResponseButtonDto>();
            CreateMap<Webhook, WebhookDto>()
                .ForMember(dest => dest.Headers, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<Dictionary<string, string>>(src.Header)));

            CreateMap<IntentUpdateDto, IntentCreateDto>();
            CreateMap<IntentQuestionUpdateDto, IntentQuestionCreateDto>();
            CreateMap<IntentQuestionSelectedKeywordUpdateDto, IntentQuestionSelectedKeywordCreateDto>();
            CreateMap<IntentAnswerInChannelUpdateDto, IntentAnswerInChannelCreateDto>();
            CreateMap<AnswerInChannelAuthenticationRequestUpdateDto, AnswerInChannelAuthenticationRequestCreateDto>();
            CreateMap<AnswerInChannelLocationRequestUpdateDto, AnswerInChannelLocationRequestCreateDto>();
            CreateMap<AnswerInChannelFormUpdateDto, AnswerInChannelFormCreateDto>();
            CreateMap<AnswerInChannelFormFieldUpdateDto, AnswerInChannelFormFieldCreateDto>();
            CreateMap<AnswerInChannelFormFieldCreateDto, AnswerInChannelFormFieldUpdateDto>();
            CreateMap<AnswerInChannelPromptUpdateDto, AnswerInChannelPromptCreateDto>();
            CreateMap<AnswerInChannelPromptCreateDto, AnswerInChannelPromptUpdateDto>();
            CreateMap<QuickReplyUpdateDto, QuickReplyCreateDto>();
            CreateMap<QuickReplyItemUpdateDto, QuickReplyItemCreateDto>();
            CreateMap<ResponseUpdateDto, ResponseCreateDto>();
            CreateMap<ResponseButtonUpdateDto, ResponseButtonCreateDto>();

            CreateMap<IntentUpdateBo, Intent>();
            CreateMap<IntentQuestionUpdateBo, IntentQuestion>();
            CreateMap<IntentQuestionSelectedKeywordUpdateBo, IntentQuestionSelectedKeyword>();
            CreateMap<IntentAnswerInChannelUpdateBo, IntentAnswerInChannel>().ReverseMap();
            CreateMap<AnswerInChannelAuthenticationRequestUpdateBo, AnswerInChannelAuthenticationRequest>().ReverseMap();
            CreateMap<AnswerInChannelLocationRequestUpdateBo, AnswerInChannelLocationRequest>().ReverseMap();
            CreateMap<AnswerInChannelFormUpdateBo, AnswerInChannelForm>().ReverseMap();
            CreateMap<AnswerInChannelFormFieldUpdateBo, AnswerInChannelFormField>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Options)));
            CreateMap<AnswerInChannelFormField, AnswerInChannelFormFieldUpdateBo>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<AnswerInChannelFormField, AnswerInChannelFormFieldUpdateBo>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<AnswerInChannelFormFieldUpdateBo, AnswerInChannelFormField>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Options)));
            CreateMap<AnswerInChannelPromptUpdateBo, AnswerInChannelPrompt>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Options)));
            CreateMap<AnswerInChannelPrompt, AnswerInChannelPromptUpdateBo>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<AnswerInChannelPrompt, AnswerInChannelPromptUpdateBo>()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Options)));
            CreateMap<AnswerInChannelPromptUpdateBo, AnswerInChannelPrompt > ()
                .ForMember(dest => dest.Options, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Options)));
            CreateMap<QuickReplyUpdateBo, QuickReply>().ReverseMap();
            CreateMap<QuickReplyItemUpdateBo, QuickReplyItem>().ReverseMap();
            CreateMap<ResponseButtonUpdateBo, ResponseButton>().ReverseMap();

            //Subscription
            CreateMap<Subscription, SubscriptionDto>();
            CreateMap<ChatbotUsage, ChatbotUsageDto>();
            //smarttrigger
            CreateMap<SmartTriggerDto, SmartTrigger>().ReverseMap();
            CreateMap<SmartTriggerUpdateDto, SmartTriggerUpdateBo>();
            CreateMap<SmartTriggerCreateDto, SmartTrigger>();
            CreateMap<SmartTriggerConditionDto, SmartTriggerCondition>().ReverseMap();
            CreateMap<SmartTriggerConditionUpdateDto, SmartTriggerConditionUpdateBo>();
            CreateMap<SmartTriggerConditionCreateDto, SmartTriggerCondition>();
            CreateMap<SmartTriggerActionDto, SmartTriggerAction>().ReverseMap();
            CreateMap<SmartTriggerActionUpdateDto, SmartTriggerActionUpdateBo>();
            CreateMap<SmartTriggerActionCreateDto, SmartTriggerAction>();
            CreateMap<SmartTriggerActionUpdateBo, SmartTriggerAction>();
            CreateMap<SmartTriggerConditionUpdateBo, SmartTriggerCondition>();
            //language
            CreateMap<Language, LanguageDto>().ReverseMap();
            //Agent Assist
            CreateMap<AgentAssistDto, AgentAssist>().ReverseMap();
            CreateMap<AgentAssistUpdateDto, AgentAssistUpdateBo>();
            CreateMap<AgentAssistCreateDto, AgentAssist>();
            //Agent Assist Synonym
            CreateMap<SynonymKeywordCreateDto, SynonymKeyword>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Synonyms)));
            CreateMap<SynonymKeywordUpdateDto, SynonymKeywordUpdateBo>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.SerializeObject(src.Synonyms)));
            CreateMap<SynonymKeyword, SynonymKeywordDto>()
                .ForMember(dest => dest.Synonyms, opt => opt.MapFrom(src => JsonConvert.DeserializeObject<List<string>>(src.Synonyms)));
            //Agent Assist learning 
            CreateMap<AgentAssistLearningQuestionCreateDto, AgentAssistLearningQuestion>()
                .ForMember(dest => dest.SuggestionId, opt => opt.MapFrom(src => FormatLearningSuggestionId(src)))
                .ForMember(dest => dest.SuggestionContent, opt => opt.MapFrom(src => FormatLearningSuggestionContent(src)));
            CreateMap<AgentAssistLearningQuestion, AgentAssistLearningQuestionDto>()
                .ForMember(dest => dest.TopScoreSuggestion, opt => opt.MapFrom(src => FormatLearningSuggestionContentDto(src)));
            
        }
        Guid? FormatLearningSuggestionId(AgentAssistLearningQuestionCreateDto agentBotLearningDto)
        {
            Guid? sReslut = null;
            if (agentBotLearningDto.SuggestionType ==  EnumSuggestionType.cannedMessage)
            {
                string id = Convert.ToString(GetPropertyValue(agentBotLearningDto.TopScoreSuggestion, "id"));
                Guid temp = Guid.Empty;
                if (Guid.TryParse(id, out temp))
                {
                    sReslut = temp;
                }
            }
            else if (agentBotLearningDto.SuggestionType == EnumSuggestionType.knowledgeBase)
            {
                string articleId = Convert.ToString(GetPropertyValue(agentBotLearningDto.TopScoreSuggestion, "articleId"));
                Guid temp = Guid.Empty;
                if (Guid.TryParse(articleId, out temp))
                {
                    sReslut = temp;
                }
            }
            else if (agentBotLearningDto.SuggestionType == EnumSuggestionType.aiChatBot)
            {
                string intentId = Convert.ToString(GetPropertyValue(agentBotLearningDto.TopScoreSuggestion, "intentId"));
                Guid temp = Guid.Empty;
                if (Guid.TryParse(intentId, out temp))
                {
                    sReslut = temp;
                }
            }
            return sReslut;
        }
        string FormatLearningSuggestionContent(AgentAssistLearningQuestionCreateDto agentBotLearningDto)
        {
            string sReslut = "";
            if (agentBotLearningDto.SuggestionType != EnumSuggestionType.none && 
                agentBotLearningDto.TopScoreSuggestion != null)
            {
                sReslut = JsonConvert.SerializeObject(agentBotLearningDto.TopScoreSuggestion);
            }            
            return sReslut;
        }
        object FormatLearningSuggestionContentDto(AgentAssistLearningQuestion agentBotLearning)
        {
            object obj = null;
            if (agentBotLearning.SuggestionType == EnumSuggestionType.cannedMessage && !string.IsNullOrEmpty(agentBotLearning.SuggestionContent))
            {
                obj = JsonConvert.DeserializeObject<CannedMessageSuggestionContent>(agentBotLearning.SuggestionContent);
            }
            else if (agentBotLearning.SuggestionType == EnumSuggestionType.knowledgeBase && !string.IsNullOrEmpty(agentBotLearning.SuggestionContent))
            {
                obj = JsonConvert.DeserializeObject<KnowledgeBaseSuggestionContent>(agentBotLearning.SuggestionContent);
            }
            else if (agentBotLearning.SuggestionType == EnumSuggestionType.aiChatBot && !string.IsNullOrEmpty(agentBotLearning.SuggestionContent))
            {
                obj = JsonConvert.DeserializeObject<ChatbotSuggestionContent>(agentBotLearning.SuggestionContent);
            }
            return obj;
        }

        private bool IfObjectIncludeProperty(object obj, string field)
        {
            bool bResult = false;
            if (obj != null)
            {
                Type t = obj.GetType();
                bResult = t.GetProperties().Any(a => a.Name.ToLower() == field.ToLower());
            }
            if (!bResult)
            {
                bResult = IfJobjectIncludeProperty(obj, field);
            }
            return bResult;
        }
        private bool IfJobjectIncludeProperty(object obj, string field)
        {
            bool bResult = false;
            try
            {
                string content = obj.ToString();
                dynamic jObject = JsonConvert.DeserializeObject<dynamic>(content);
                if (jObject is JObject && jObject.Property(field) != null)
                {
                    bResult = true;
                }
            }
            catch (Exception e)
            {

            }
            return bResult;
        }
        private object GetPropertyValue(object obj, string field)
        {
            object value = null;
            if (obj != null)
            {
                Type t = obj.GetType();
                PropertyInfo pi = t.GetProperties().FirstOrDefault(a => a.Name.ToLower() == field.ToLower());
                value = pi?.GetValue(obj);
            }

            if (value == null && IfJobjectIncludeProperty(obj, field))
            {
                value = GetPropertyValueInJobject(obj, field);
            }

            return value;
        }
        private object GetPropertyValueInJobject(object obj, string field)
        {
            object value = null;
            try
            {
                string content = obj.ToString();
                dynamic jObject = JsonConvert.DeserializeObject<dynamic>(content);
                if (jObject is JObject)
                {
                    value = jObject[field].Value;
                }
            }
            catch (Exception e)
            {

            }
            return value;
        }
        private List<IntentQuestionsWithEntityDto> FormatQuestions(ICollection<IntentQuestion> intentQuestions)
        {
            //转成engine支持的Question
            List<IntentQuestionsWithEntityDto> intentQuestionsWithEntityDtos = new List<IntentQuestionsWithEntityDto> { };
            return intentQuestionsWithEntityDtos;
        }

        private Dictionary<string, IntentAnswerSubItemDto> FormatAnswerInChannels(ICollection<IntentAnswerInChannel> intentAnswerInChannels)
        {
            //转成engine支持的Answer
            Dictionary<string, IntentAnswerSubItemDto> answers = new Dictionary<string, IntentAnswerSubItemDto> { };
            return answers;
        }
    }
}
